import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

public class Task extends TimerTask {
	String cmd;

	public Task(String cmd) {
		this.cmd = cmd;
	}
	
	private static String getDate(String fmt) {
		if (fmt == null || fmt.equals("")) {
			fmt = "yyyy-MM-dd";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		return sdf.format(new Date());
	}

	@Override
	public void run() {
		Runtime rt = Runtime.getRuntime();
		try {
			System.out.println("Execute:" + getDate("yyyy-MM-dd HH:mm:ss"));
			Process proc = rt.exec(cmd);
			InputStream stderr = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(stderr,"GBK");
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null)
				System.out.println(line);
			System.out.println("");
			int exitVal = proc.waitFor();
			if(exitVal==0){
				proc.destroy();
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

}
